using System;
using System.Runtime.InteropServices;
using System.Text;

namespace daq_test_cs
{
    public class DAQ125Wrapper
    {
        // 触发类型
        public enum LockzhinerADCTriggerType : int
        {
            Normal = 0x00, // 正常模式或滚动模式（不进行触发）
            RisingEdge = 0x01, // 上升沿
            FallingEdge = 0x02, // 下降沿
            DoubleEdge = 0x03, //  双边缘
            ExternalTrigger = 0x04, // 外部触发
            Error = 0x05,
        }

        // 触发模式
        public enum LockzhinerADCTriggerMode : int
        {
            Automatic = 0x00, // 自动触发
            Common = 0x01,  // 普通触发
            Single = 0x02,  // 单次触发
            Error = 0x04,
        }

        // 通道枚举
        public enum LockzhinerADCChannel : int
        {
            ADCChannelAINNone = 0b00000000, // 全不选 | 软件触发
            ADCChannelAIN1 = 0b00000001, // ADC通道1
            ADCChannelAIN2 = 0b00000001 << 1, // ADC通道2
            ADCChannelAIN3 = 0b00000001 << 2, // ADC通道3
            ADCChannelAIN4 = 0b00000001 << 3, // ADC通道4
            ADCChannelAIN5 = 0b00000001 << 4, // ADC通道5
            ADCChannelAIN6 = 0b00000001 << 5, // ADC通道6
            ADCChannelAIN7 = 0b00000001 << 6, // ADC通道7
            ADCChannelAIN8 = 0b00000001 << 7, // ADC通道8
        }

        // 采样率
        public enum LockzhinerADCSampleRate : int
        {
            ADCSampleRate_None = 0, // 未知采样率
            ADCSampleRate_1 = 1,             // 1采样率
            ADCSampleRate_10 = 10, // 10采样率
            ADCSampleRate_100 = 100, // 100采样率
            ADCSampleRate_200 = 200, // 200采样率
            ADCSampleRate_500 = 500, // 500采样率
            ADCSampleRate_1_K = 1000, // 1K采样率
            ADCSampleRate_2_K = 2000, // 2K采样率
            ADCSampleRate_5_K = 5000, // 5K采样率
            ADCSampleRate_10_K = 10000, // 10K采样率
            ADCSampleRate_20_K = 20000, // 20K采样率
            ADCSampleRate_50_K = 50000, // 50K采样率
            ADCSampleRate_100_K = 100000, // 100K采样率
            ADCSampleRate_200_K = 200000, // 200K采样率
            ADCSampleRate_500_K = 500000, // 500K采样率
            ADCSampleRate_800_K = 800000, // 800K采样率
            ADCSampleRate_1_M = 1000000, // 1M采样率
            ADCSampleRate_2_M = 2000000, // 2M采样率
            ADCSampleRate_5_M = 5000000, // 5M采样率
            ADCSampleRate_10_M = 10000000, // 10M采样率
            ADCSampleRate_20_M = 20000000, // 20M采样率
            ADCSampleRate_50_M = 50000000, // 50M采样率
            ADCSampleRate_65_M = 65000000, // 65M采样率
            ADCSampleRate_100_M = 100000000, // 100M采样率
            ADCSampleRate_125_M = 125000000, // 125M采样率
            ADCSampleRate_250_M = 250000000  // 250M采样率
        }

        // 电压量程
        public enum LockzhinerADCVoltage : int
        {
            ADCVoltage_None = -1,     // 未知电压量程
            ADCVoltage_0_1_V = 0,     // 0.1V电压量程
            ADCVoltage_0_2_V = 1,     // 0.2V电压量程
            ADCVoltage_0_5_V = 2,     // 0.5V电压量程
            ADCVoltage_1_V = 3,       // 1V电压量程
            ADCVoltage_2_V = 4,       // 2V电压量程
            ADCVoltage_2_5_V = 5,     // 2.5V电压量程
            ADCVoltage_5_V = 6,       // 5V电压量程
            ADCVoltage_10_V = 7,      // 10V电压量程
            ADCVoltage_15_V = 8,      // 15V电压量程
            ADCVoltage_20_V = 9,      // 20V电压量程
            ADCVoltage_50_V = 10,     // 50V电压量程
            ADCVoltage_60_V = 11,     // 60V电压量程
            ADCVoltage_100_V = 12     // 100V电压量程
        }

        private const string DllPath = "./libdaq-4.0.0.dll";

        // 4. 设备相关接口
        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        public static extern IntPtr DAQ125_New();

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        public static extern void DAQ125_Delete(IntPtr device);
    
        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_UseBackend(IntPtr device, IntPtr backend);
       
        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_InitializeDevice(IntPtr device);

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_ConnectedDevice(IntPtr device);

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_DisconnectedDevice(IntPtr device);

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_GetDeviceName(IntPtr device, StringBuilder nameBuffer, int bufferSize);

        // 5. 数据采集相关接口
        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_StartCollection(IntPtr device);

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_StopCollection(IntPtr device);

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_TryReadData(IntPtr baseClass, byte channel, 
           [MarshalAs(UnmanagedType.LPArray)] float[] readBuffer, uint readSize, uint timeout);

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_TryReadData_Point(IntPtr baseClass, byte channel,
           [MarshalAs(UnmanagedType.LPArray)] float[] readBuffer);

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_TryReadData_Batch(IntPtr baseClass,byte channel, 
                [MarshalAs(UnmanagedType.LPArray)] float[] readBuffer, uint readSize);

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_ClearDataBuffer(IntPtr baseClass);

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_ClearDataBuffer_Channel(IntPtr baseClass, byte channel);

        // 6. 基本信息相关接口
        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_BasicInfoGetDeviceType(IntPtr baseClass);

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_GetDeviceType(IntPtr baseClass, StringBuilder typeBuffer, int bufferSize);

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_BasicInfoGetHardwareVersion(IntPtr baseClass, StringBuilder versionBuffer, int bufferSize);

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_GetHardwareVersion(IntPtr baseClass, StringBuilder versionBuffer, int bufferSize);

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_BasicInfoGetBootLoaderVersion(IntPtr baseClass, StringBuilder versionBuffer, int bufferSize);

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_GetBootLoaderVersion(IntPtr baseClass, StringBuilder versionBuffer, int bufferSize);

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_BasicInfoGetSoftwareVersion(IntPtr baseClass, StringBuilder versionBuffer, int bufferSize);

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_GetSoftwareVersion(IntPtr baseClass, StringBuilder versionBuffer, int bufferSize);

        // 7. ADC 配置相关接口
        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_ConfigADCSampleRate(IntPtr baseClass, int sampleRate);

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_ConfigADCVoltage_Batch(IntPtr baseClass, 
            [MarshalAs(UnmanagedType.LPArray)] int[] rangeArray, int arraySize);

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_ConfigADCVoltage(IntPtr baseClass, int voltage);

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_ConfigADCTrigger(IntPtr device,
                                                         int type,
                                                         int mode,
                                                         int channel,
                                                         int level);
 
        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_ConfigADCTriggerLength(IntPtr baseClass, uint dataLength);
    
        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_ConfigTriggerTimeout(IntPtr baseClass, uint timeout);
    
        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_ConfigADCChannel(IntPtr baseClass, byte channel);

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_ConfigADCChannel_Batch(IntPtr baseClass, 
            [MarshalAs(UnmanagedType.LPArray)] bool[] enableArray, int arraySize);
    
        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_ConfigACDC(IntPtr baseClass, byte channel);

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_ConfigADCTriggerSource(IntPtr baseClass);

        // 7.1 设备信息查询接口
        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_GetADCLegalSampleRate(IntPtr baseClass, 
            [MarshalAs(UnmanagedType.LPArray)] uint[] rateArray, ref int arraySize);

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        public static extern uint DAQ125_GetCurrentSampleRate(IntPtr baseClass);

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_GetADCLegalChannel(IntPtr baseClass, 
            [MarshalAs(UnmanagedType.LPArray)] uint[] channelArray, ref int arraySize);

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_GetADCLegalVoltage(IntPtr baseClass, 
            [MarshalAs(UnmanagedType.LPArray)] uint[] voltageArray, ref int arraySize);

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_GetADCCurrentVoltage(IntPtr baseClass, 
            [MarshalAs(UnmanagedType.LPArray)] uint[] voltageArray, ref int arraySize);

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        public static extern byte DAQ125_GetADCLegalChannelSize(IntPtr baseClass);

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_GetCurrentTriggerType(IntPtr baseClass, ref uint triggerType);

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_GetCurrentTriggerMode(IntPtr baseClass, ref uint triggerMode);

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        public static extern byte DAQ125_GetChannelEnableState(IntPtr baseClass);

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_GetChannelEnableList(IntPtr baseClass, 
            [MarshalAs(UnmanagedType.LPArray)] bool[] enableArray, ref int arraySize);

        // 7.2 数据缓冲区管理接口
        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        public static extern uint DAQ125_GetADCBufferDataSize(IntPtr baseClass, int channel);

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        public static extern uint DAQ125_GetADCBufferDataMaxSize(IntPtr baseClass);

        // 7.3 触发长度限制查询接口
        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        public static extern uint DAQ125_GetADCLegalLengthMin(IntPtr baseClass);

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        public static extern uint DAQ125_GetADCLegalLengthMax(IntPtr baseClass);

        // 7.4 参数合法性验证接口
        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_ADCSampleRateIsLegal(IntPtr baseClass, uint sampleRate);

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_ADCRangeIsLegal(IntPtr baseClass, int range);

        // 8. 系统操作
        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_GetServerInfo(IntPtr baseClass, StringBuilder ipBuffer, int ipBufferSize, ref uint port);

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_SystemOpGetDeviceName(IntPtr baseClass, StringBuilder nameBuffer, int bufferSize);

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_ProductTestGetSN(IntPtr baseClass, StringBuilder snBuffer, int bufferSize);

        [DllImport(DllPath, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DAQ125_GetSN(IntPtr baseClass, StringBuilder snBuffer, int bufferSize);
    }
} 